package com.crm.controller.settings;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.UserDAO;
import com.crm.entity.Business;
import com.crm.entity.User;
import com.crm.util.EncriptAndDecript;
import com.mysql.jdbc.StringUtils;

@Controller
@RequestMapping("/account")
public class MyAccount {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping("/show.do")
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		return model=new ModelAndView("/jsp/myaccount.jsp")
		.addObject("submenu", "reports")
		.addObject("menuName","reports");
		
	}
	@RequestMapping("/update.do")
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		User us=(User)session.getAttribute("user");
		String sid=request.getParameter("userId");
		String fname=request.getParameter("fname");
		String lname=request.getParameter("lname");
		String email=request.getParameter("email");
		String emailpwd=request.getParameter("emailpwd");
		String uname=request.getParameter("uname");
		String oldpwd=request.getParameter("oldpwd");
		String newpwd=request.getParameter("newpwd");
		String confpwd=request.getParameter("confpwd");
		
		byte[] encrypted=EncriptAndDecript.encrypt(emailpwd);
		System.out.println("Encrypted Email Pwd Is:"+encrypted);
		
		
		String decrypted=EncriptAndDecript.decrypt(encrypted);
		System.out.println("Decrepted Email Pwd Is:"+decrypted);
		
		boolean newpwdval = StringUtils.isNullOrEmpty(newpwd);
		boolean confpwdval=StringUtils.isNullOrEmpty(confpwd);
		if(newpwdval==true && confpwdval==true){
			newpwd=oldpwd;
			confpwd=oldpwd;
		}
		
		int id=Integer.parseInt(sid);
		int role=us.getRole();
		if(newpwd.equals(confpwd) && !oldpwd.equals(null) && us.getPassword().equals(oldpwd)){
			User u=new User(id,bus,uname,newpwd,fname,lname,email,emailpwd,role);
			
			UserDAO udao=new UserDAO();
			udao.insert(u);
			session.removeAttribute("user");
			session.setAttribute("user",u);
			
			return model=new ModelAndView("/jsp/myaccount.jsp")
			.addObject("submenu", "reports")
			.addObject("menuName","reports")
			.addObject("msg","Successfully Updated The Details");
		}else{
			return model=new ModelAndView("/jsp/myaccount.jsp")
			.addObject("submenu", "reports")
			.addObject("menuName","reports")
			.addObject("msg","New Password And Confirm Passwords Are Not Matching");
		}
		
	}
	
}
